package com.schedualnew.mq.util;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;


public class FileUtils {
    private static Logger log = LoggerFactory.getLogger(FileUtils.class);

    public static String getFilePath(String fileName) {
        ClassPathResource classPathResource = new ClassPathResource(fileName);
        String path = "";
        try {
            path = classPathResource.getFile().getAbsolutePath();
        } catch (IOException e1) {
            e1.printStackTrace();
            log.error(e1.getMessage());
        }
        return path;
    }


    public static String readFromFile(String filePath) {
        String encoding = "UTF-8";
        File file = new File(filePath);
        Long filelength = Long.valueOf(file.length());
        byte[] filecontent = new byte[filelength.intValue()];
        try {
            FileInputStream in = new FileInputStream(file);
            in.read(filecontent);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        try {
            return new String(filecontent, encoding);
        } catch (UnsupportedEncodingException e) {
            System.err.println("The OS does not support " + encoding);
            e.printStackTrace();
            log.error(e.getMessage());
            return null;
        }
    }
}
